//
//  ChartSportCell.swift
//  TimeBoat
//
//  Created by bo wang on 2022/8/28.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import UIKit
import SDWebImage

class ChartSportCell: UITableViewCell {
    @IBOutlet var container: UIView!
    @IBOutlet var iconImageView: UIImageView!
    @IBOutlet var sportNameLabel: UILabel!
    @IBOutlet var sportSummaryLabel: UILabel!
    @IBOutlet var sportDateLabel: UILabel!
    @IBOutlet var arrowImageView: UIImageView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        container.layer.masksToBounds = true
        container.layer.cornerRadius = 16
        sportDateLabel.isHidden = true
    }
    
    func update(data: SportTotalDataSportUnit) {
        setup(icon: data.sportIcon, name: data.sportName)

        var summary = [String]()
        if let str = getCalorieStr(Double(data.totalCalorie)) {
            summary.append(str)
        } else {
            summary.append("0.00千卡")
        }
        
        if let str = getTimeStr(data.totalTime) {
            summary.append(str)
        } else {
            summary.append("0秒")
        }
        
        summary.append("\(data.count)次")
        sportSummaryLabel.text = summary.joined(separator: " ")
    }
    
    func update(data: SportTotalDataSportUnit, detail: SportDetailDataUnit) {
        setup(icon: data.sportIcon, name: data.sportName)
        
        var summary = [String]()
        if let str = getDistanceStr(Int(detail.actualDistance)) {
            summary.append(str)
        }
        if let str = getStepsStr(detail.totalStep) {
            summary.append(str)
        }
        if let str = getCalorieStr(detail.calorie) {
            summary.append(str)
        }
        if let str = getTimeStr(Int(detail.actualUseTime)) {
            summary.append(str)
        }
        sportSummaryLabel.text = summary.joined(separator: " ")
        
        sportDateLabel.text = detail.date
        sportDateLabel.isHidden = false
        if let d = detail.d {
            sportDateLabel.text = String.init(format: "%02d-%02d %02d:%02d:%02d", d.month, d.day, d.hour, d.minute, d.second)//"\(d.month)-\(d.day) \(d.hour):\(d.minute):\(d.second)"
            sportDateLabel.isHidden = false
        } else {
            sportDateLabel.isHidden = true
        }
        arrowImageView.isHidden = true
    }
    
    func setup(icon: String?, name: String?) {
        if let icon = icon {
            iconImageView.sd_setImage(with: URL(string: icon))
        } else {
            iconImageView.image = nil
        }
        sportNameLabel.text = name
    }
    
    private func getTimeStr(_ time: Int) -> String? {
        guard time > 0 else { return nil }
        
        let hours = time / 3600
        let minutes = time % 3600 / 60
        let seconds = time % 60
        if hours > 0 {
            if minutes == 0 && seconds == 0 {
                return "\(hours)小时"
            } else if seconds == 0 {
                return "\(hours)小时\(minutes)分"
            } else {
                return "\(hours)小时\(minutes)分\(seconds)秒"
            }
        } else {
            if minutes == 0 {
                return "\(seconds)秒"
            } else if seconds == 0 {
                return "\(minutes)分"
            } else {
                return "\(minutes)分\(seconds)秒"
            }
        }
    }
    
    private func getDistanceStr(_ distance: Int) -> String? {
        guard distance > 0 else { return nil }
        if distance >= 1000 {
            return String(format: "%.2f千米", Double(distance) / 1000)
        } else {
            return "\(distance)米"
        }
    }
    
    private func getStepsStr(_ step: Int) -> String? {
        guard step > 0 else { return nil }
        return "\(step)步"
    }
    
    private func getCalorieStr(_ calorie: Double) -> String? {
        guard calorie > 0 else { return nil }
        return String(format: "%.2f千卡", calorie / 1000)
    }
}
